#include "iostream"
#include "fstream"
using namespace std;
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
const char *filePath = "D:\\save.bin";
typedef unsigned char BYTE;

static char* s_cGlobalPlayer1;
static char* s_cGlobalPlayer2;
static char* s_cGlobalBoard;
static bool s_bGlobalSwithPlayer;

BYTE *fileBufTemp;// Pointer to our buffered data
void SwitchPlayer();
void Tictoctoe();
bool IsCheckWon();
void InputNamePlayer();
void InGame();
void ShowBoard();
void LoadDataSave();
long getFileSize(FILE *file);
void ShowReplay();
void SwitchPlayer()
{
	int localMode;
	printf("\n SELECT YOUR MODE (1 -PLAY GAME, 2 - REPLAY GAME, OTHERS - EXIT GAME):");
	scanf("%d", &localMode);
	switch(localMode)
	{
	case 1 : InputNamePlayer();
			 ShowBoard();
			 InGame();
			break;
	case 2 : 

			break;
	default : exit (EXIT_FAILURE);
	}
}
void ReplayGame()
{
	FILE * pFile;
    pFile = fopen ("save.bin","r+");
	char tmp;
	char a[n*n];
	if(isSaveWin == true){
		 for(int i = 0; i < n*n; i++)
		{
			fscanf (pFile, "%c", &s_cGlobalBoard[i]);
		}
	}
	else 
	{
		while(1)
		{
			fscanf (pFile, "%c", &tmp);
			arrPointRead.push_back(tmp);
			if(feof(pFile))
				break;
		}
	}
	
    fclose (pFile);
   

}


void InGame()
{
	char* localPosition = new char[];
	
	while(true)
	{
		cout << "\n Your turn "<<(s_bGlobalSwithPlayer ? s_cGlobalPlayer1 : s_cGlobalPlayer2)<<(s_bGlobalSwithPlayer ?" O:" : " X:" );
		cin >> localPosition;
		cout << "\n localPosition"<<(sizeof(localPosition)/sizeof(localPosition[1]));
			
			cout << "\n You went right way!";
			cout << "Position 0 :"<<localPosition[0];
			cout << "Position 1 :"<<localPosition[1];
			int i = localPosition[0] - '0' -1;
			int j = localPosition[1] - '0' -1;
			int temp = localPosition[2] - '0' -1;
			
			
			if(s_bGlobalSwithPlayer)
				s_cGlobalBoard[i*2+j] = 'O';
			else
			s_cGlobalBoard[i*2+j] = 'X';
			ShowBoard();
			if(IsCheckWon())
			{
				cout << "Congratulation "<<(s_bGlobalSwithPlayer ? s_cGlobalPlayer1 : s_cGlobalPlayer2);
				cout << "\nYou won!!!";
				break;
			}	
			s_bGlobalSwithPlayer = !s_bGlobalSwithPlayer;

		
	}

}
void ShowBoard()
{
	printf("\n\n");
	for(int i = 0; i < 9 ; i++ )
	{
		if(i%3==0)
		printf("\n");
		printf(" ");
		printf("%c ",s_cGlobalBoard[i]);
		printf(" ");
	}
}
void InputNamePlayer()
{
	cout << "Enter Player 1 Name:";
	cin >> s_cGlobalPlayer1 ;
	cout << "Enter Player 2 Name:";
	cin >> s_cGlobalPlayer2 ;
	//printf("s_cGlobalPlayer1=%x",&s_cGlobalPlayer1);
	cout << "Name1="<<s_cGlobalPlayer1<<endl;
	cout << "Name2="<<s_cGlobalPlayer2<<endl;
	
}
bool IsCheckWon()
{
	if(s_cGlobalBoard[0]==s_cGlobalBoard[1] && s_cGlobalBoard[1]==s_cGlobalBoard[2] && s_cGlobalBoard[0] != 'T')
		return true;
	else if(s_cGlobalBoard[3]==s_cGlobalBoard[4] && s_cGlobalBoard[4]==s_cGlobalBoard[5] && s_cGlobalBoard[5] != 'T' )
		return true;
	else if(s_cGlobalBoard[6]==s_cGlobalBoard[7] && s_cGlobalBoard[7]==s_cGlobalBoard[8] && s_cGlobalBoard[8] != 'T' )
		return true;
	else if(s_cGlobalBoard[0]==s_cGlobalBoard[3] && s_cGlobalBoard[3]==s_cGlobalBoard[6] && s_cGlobalBoard[6] != 'T')
		return true;
	else if(s_cGlobalBoard[1]==s_cGlobalBoard[4] && s_cGlobalBoard[4]==s_cGlobalBoard[7] && s_cGlobalBoard[7] != 'T')
		return true;
	else if(s_cGlobalBoard[2]==s_cGlobalBoard[5] && s_cGlobalBoard[5]==s_cGlobalBoard[8] && s_cGlobalBoard[8] != 'T' )
		return true;
	else if(s_cGlobalBoard[0]==s_cGlobalBoard[4] && s_cGlobalBoard[4]==s_cGlobalBoard[8] && s_cGlobalBoard[8] != 'T')
		return true;
	else if(s_cGlobalBoard[2]==s_cGlobalBoard[4] && s_cGlobalBoard[4]==s_cGlobalBoard[6] && s_cGlobalBoard[6] != 'T')
		return true;
	
	else return false;

}
void Tictoctoe()
{
	s_cGlobalBoard = new char[9];
	s_bGlobalSwithPlayer = true;
	for(int i=0;i<9;i++)
	{
		s_cGlobalBoard[i] = 'T';
	}
	s_cGlobalPlayer1 = new char[];
	s_cGlobalPlayer2 = new char[];
	

}
int main()
{
	printf("Welcome To Tic-tac-toe game! Play with your way!");
	printf("\n");
	printf("\n");
	printf("\n");
	printf("If you find any problem, please contact tho.lekhac@gameloft.com");
//Add code
	Tictoctoe();
	//InputNamePlayer();
	//ShowBoard();
	SwitchPlayer();
	//ReplayGame();
	printf("\n");
	printf("\n");
	printf("\n");
	getch();
}